package com.han.adbconnect;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.han.adbconnect.utils.NetworkUtils;
import com.han.adbconnect.utils.ProgressUtil;
import com.han.adbconnect.utils.ShellUtils;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TextView tv_ip;
    private TextView tv_type;
    private TextView btn_implement;
    private TextView btn_quit;


    Handler handler = new Handler() {
        public void handleMessage(Message param1Message) {
            MainActivity.this.progressUtil.hide();
            if (((ShellUtils.CommandResult) param1Message.obj).result == 0) {
                MainActivity.this.tv_type.setText(String.format("状态：%s", "success!"));
                MainActivity.this.tv_type.setTextColor(Color.GREEN);
                return;
            }
            MainActivity.this.tv_type.setText(String.format("状态：%s", "failed!"));
            MainActivity.this.tv_type.setTextColor(Color.RED);
        }
    };

    private ProgressUtil progressUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_ip = findViewById(R.id.tv_ip);
        tv_type = findViewById(R.id.tv_type);
        btn_implement = findViewById(R.id.btn_implement);
        btn_quit = findViewById(R.id.btn_quit);
        this.progressUtil = new ProgressUtil(this);
        tv_ip.setText("本地IP:" + NetworkUtils.getIPAddress());

        btn_implement.setOnClickListener(view -> {
            this.progressUtil.show("");
            (new Thread(() -> {
                ArrayList<String> arrayList = new ArrayList();
                arrayList.add("setprop service.adb.tcp.port 5555");
                arrayList.add("stop adbd");
                arrayList.add("start adbd");
                ShellUtils.CommandResult commandResult = ShellUtils.execCommand(arrayList, true);
                Message message = new Message();
                message.obj = commandResult;
                MainActivity.this.handler.sendMessage(message);
            })).start();
        });
        btn_quit.setOnClickListener(view -> finish());
    }
}